package com.ruicar.afs.cloud.manage.creditchange.controller;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruicar.afs.cloud.common.core.util.IResponse;
import com.ruicar.afs.cloud.common.util.EmptyUtils;
import com.ruicar.afs.cloud.manage.common.entity.ManageRelationChange;
import com.ruicar.afs.cloud.manage.common.service.ManageRelationChangeService;
import com.ruicar.afs.cloud.manage.outer.api.change.relation.dto.RelationChangeDTO;
import com.ruicar.afs.cloud.manage.workflow.processor.ApproveTaskProcessor;
import io.micrometer.core.instrument.util.StringUtils;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;

/**
 * @Author chuke
 * @create 2020/7/4 13:58
 * 联系方式和地址变更
 */
@Slf4j
@RestController
@RequestMapping("/contactChange")
@AllArgsConstructor
public class ContactChangeController {
    private final ManageRelationChangeService manageRelationChangeService;
    private final ApproveTaskProcessor approveTaskProcessor;

    @ApiOperation(value = "联系方式申请或暂存过的案件列表")
    @PostMapping("/getManageContactChangeList")
    public IResponse getManageContractChangeContractNo(@RequestBody RelationChangeDTO dto) {
        String endCreateDate = null;
        String endFinishDate = null;
        if (dto.getEndCreateDate() != null) {
            long getEndCreateDate = dto.getEndCreateDate().getTime() + 24 * 60 * 60 * 1000;
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            endCreateDate = simpleDateFormat.format(getEndCreateDate);
        }
        if (dto.getEndFinishDate() != null) {
            long getEndFinishDate = dto.getEndFinishDate().getTime() + 24 * 60 * 60 * 1000;
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            endFinishDate = simpleDateFormat.format(getEndFinishDate);
        }
        Page page = manageRelationChangeService.page(new Page(dto.getPageNumber(), dto.getPageSize())
                , Wrappers.<ManageRelationChange>query().lambda()
                        .like(StringUtils.isNotEmpty(dto.getApplyNo()), ManageRelationChange::getApplyNo, dto.getApplyNo())
                        .like(StringUtils.isNotEmpty(dto.getContractNo()), ManageRelationChange::getContractNo, dto.getContractNo())
                        .like(StringUtils.isNotEmpty(dto.getCustName()), ManageRelationChange::getCustName, dto.getCustName())
                        .ge(EmptyUtils.isNotEmpty(dto.getStartCreateDate()), ManageRelationChange::getApplyDate, dto.getStartCreateDate())
                        .le(EmptyUtils.isNotEmpty(dto.getEndCreateDate()), ManageRelationChange::getApplyDate, endCreateDate)
                        .ge(EmptyUtils.isNotEmpty(dto.getStartFinishDate()), ManageRelationChange::getEndDate, dto.getStartFinishDate())
                        .le(EmptyUtils.isNotEmpty(dto.getEndFinishDate()), ManageRelationChange::getEndDate, endFinishDate)
                        .eq(EmptyUtils.isNotEmpty(dto.getBusinessSource()), ManageRelationChange::getBusinessSource, dto.getBusinessSource())
                        .eq(EmptyUtils.isNotEmpty(dto.getProcessStatus()), ManageRelationChange::getProcessStatus, dto.getProcessStatus())
                        .eq(EmptyUtils.isNotEmpty(dto.getChannelId()), ManageRelationChange::getChannelId, dto.getChannelId())
                        .orderByDesc(ManageRelationChange::getApplyDate));
        return new IResponse().setData(page.setRecords(approveTaskProcessor.handle(page.getRecords())));
    }

    @ApiOperation(value = "查询新的客户联系方式")
    @PostMapping("/findContractNewContact")
    public IResponse findRelationChangeInfo(@RequestParam("contractNo") String contractNo,
                                            @RequestParam("caseNo") String caseNo) {
        ManageRelationChange one = manageRelationChangeService.getOne(Wrappers.<ManageRelationChange>query().lambda().eq(ManageRelationChange::getContractNo, contractNo)
                .eq(ManageRelationChange::getCaseNo, caseNo));
        return IResponse.success(one.getChangeInfoJson());
    }

    @Transactional(rollbackFor = Exception.class)
    @ApiOperation(value = "客户联系方式或地址变更")
    @PostMapping("/changeContactSubmit")
    public IResponse changeContactSubmit(@RequestBody Map map) {
        return IResponse.success(manageRelationChangeService.contactChangeSubmit(map));
    }

    @ApiOperation(value = "客户联系方式或地址变更提交审批")
    @PostMapping("/changeContactChange")
    public IResponse changeContactChange(@RequestBody Map map) {
        if (manageRelationChangeService.contactChangeSave(map)) {
            return IResponse.success("保存成功").setData(map);
        } else {
            return IResponse.fail("保存失败");
        }
    }

    @ApiOperation(value = "删除联系方式变更草稿数据")
    @PostMapping("/delete")
    public IResponse delete(@RequestParam("id") Long id) {
        if (manageRelationChangeService.removeById(id)) {
            return IResponse.success("删除成功");
        } else {
            return IResponse.fail("删除失败");
        }
    }

}
